package com.gaojinqi.sys.modular.rbac.service.impl;

import com.gaojinqi.base.common.enums.StateEnum;
import com.gaojinqi.base.common.service.imp.BaseTreeServiceImpl;
import com.gaojinqi.sys.core.shiro.config.ShiroFilterConfiguration;
import com.gaojinqi.sys.modular.rbac.dao.PermissionRepository;
import com.gaojinqi.sys.modular.rbac.dao.RolePermissionRepository;
import com.gaojinqi.sys.modular.rbac.model.Permission;
import com.gaojinqi.sys.modular.rbac.model.Role;
import com.gaojinqi.sys.modular.rbac.model.RolePermissionMapping;
import com.gaojinqi.sys.modular.rbac.service.PermissionService;
import com.google.common.collect.Lists;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Sort;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;

import java.util.ArrayList;
import java.util.Collection;
import java.util.List;

import static com.gaojinqi.sys.modular.rbac.model.Permission.SORT_FIELD;

/**
 * @author gaojinqi
 * @version 1.0
 * @since 2020年04月21日
 */
@Service
public class PermissionServiceImpl extends BaseTreeServiceImpl<Permission, String> implements PermissionService {

    @Autowired
    private PermissionRepository permissionRepository;
    @Autowired
    private RolePermissionRepository rolePermissionRepository;
    @Autowired
    private ShiroFilterConfiguration shiroFilterConfiguration;

    @Override
    public List<Permission> findEnablePermsByRoleId(String roleId) {
        return rolePermissionRepository.findPermsByRoleIdAndState(roleId, StateEnum.ENABLE.getCode());
    }

    @Override
    public List<Permission> findEnablePermsByRoleId(Collection<String> roleIds) {
        return rolePermissionRepository.findPermsByRoleIdsAndState(roleIds, StateEnum.ENABLE.getCode());
    }

    @Override
    public Permission findByPermission(String permission) {
        return permissionRepository.findByPermission(permission);
    }

    @Override
    public List<Permission> findAll() {
        return permissionRepository.findAll(Sort.by(Sort.Order.asc(SORT_FIELD)));
    }

    @Override
    @Transactional
    public void deleteById(String permId) {
        // 删除角色权限关系
        rolePermissionRepository.deleteByPermId(permId);
        // 删除权限
        super.deleteById(permId);
        // 刷新权限
        shiroFilterConfiguration.reloadFilterChain();
    }

    @Override
    @Transactional
    public void deleteByIds(List<String> permIds) {
        // 删除角色权限关系
        rolePermissionRepository.deleteByPermId(permIds);
        // 删除权限
        super.deleteByIds(permIds);
        // 刷新权限
        shiroFilterConfiguration.reloadFilterChain();
    }

    @Override
    public List<Permission> findPermsByRoleId(String roleId) {
        return rolePermissionRepository.findPermsByRoleId(roleId);
    }

    @Override
    @Transactional
    public void assignPermsToRoleId(String roleId, Collection<String> permIds) {
        // 删除指定角色的已分配的权限
        rolePermissionRepository.deleteByRoleId(roleId);
        // 重新给指定角色分配权限
        List<RolePermissionMapping> mappingList = Lists.newArrayList();
        Role role = new Role();
        role.setRoleId(roleId);
        for (String permId : permIds) {
            RolePermissionMapping mapping = new RolePermissionMapping();
            mapping.setRole(role);
            Permission perm = new Permission();
            perm.setPermissionId(permId);
            mapping.setPerm(perm);
            mappingList.add(mapping);
        }
        if (!CollectionUtils.isEmpty(mappingList)) {
            rolePermissionRepository.saveSelective(mappingList);
            // 刷新规则
            shiroFilterConfiguration.reloadFilterChain();
        }
    }

}
